/**
 * 
 */
package org.jft.datamodel.database.mapping;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

/**
 * @author LAP
 *
 */
@Entity
@Table(name = "person")
public class Person {
	
	@Id
	@GeneratedValue(generator = "Increment")
	@GenericGenerator(name = "Increment", strategy = "Increment")
	@Column(name = "person_id", nullable = false)
	private Long personId;
	
	@Column(name = "name", nullable = false)
	private String name;
	
	@Column(name = "sex", nullable = false)
	private Boolean sex;
	
	@Column(name = "birth_date", nullable = false)
	private Long birthDate;
	
	@Column(name = "photo_id")
	private Long photoId;
	
	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "photo_id")
	private Photo photo;
	
	@ManyToMany
	@JoinTable(
			name = "person_states",
			joinColumns = {@JoinColumn(name = "person_id", referencedColumnName = "person_id")},
			inverseJoinColumns = {@JoinColumn(name = "state_id", referencedColumnName = "state_id")})
	private List<State> states;
	
	@ManyToMany(cascade = CascadeType.ALL)
	@JoinTable(
			name = "person_events",
			joinColumns = {@JoinColumn(name = "person_id", referencedColumnName = "person_id")},
			inverseJoinColumns = {@JoinColumn(name = "event_id", referencedColumnName = "event_id")})
	private List<Event> events;
	
	@OneToMany(mappedBy = "firstPerson")
	private List<Couple> firstPersonCouples;
	
	@OneToMany(mappedBy = "secondPerson")
	private List<Couple> secondPersonCouples;
	
	@OneToMany(mappedBy = "person")
	private List<Parents> parents;
	
	public Person() {
		name = "Uncnown";
		sex = false;
		birthDate = new Date().getTime();
	}
	
	public Person(Person value) {
		name = value.getName();
		sex = value.getSex();
		birthDate = value.getDateOfBirth();
	}
	
	public Long getID() {
		return personId;
	}
	
	public String getName() {
		return name;
	}
	
	public Long getPhotoId() {
		return photoId;
	}

	public Boolean getSex() {
		return sex;
	}
	
	public Long getDateOfBirth() {
		return birthDate;
	}
	
	public Photo getPhoto() {
		return photo;
	}
	
	public List<State> getStates() {
		return states;
	}
	
	public List<Event> getEvents() {
		return events;
	}
	
	public void setID(Long value) {
		personId = value;
	}
	
	public void setName(String value) {
		name = value;
	}
	
	public void setSex(Boolean value) {
		sex = value;
	}
	
	public void setDateOfBirth(Long value) {
		birthDate = value;
	}
	
	public void setPhoto(Photo value) {
		photo = value;
		setPhotoId(value.getId());
	}
	
	public void setPhotoId(Long value) {
		photoId = value;
	}
	
	public void addState(State value) {
		if(!states.contains(value)) {
			states.add(value);
		}
	}
	
	public void removeState(State value) {
		states.remove(value);
	}
	
	public void addEvent(Event value) {
		events.add(value);
	}
	
	public void removeEvent(Event value) {
		events.remove(value);
	}
	
}
